/* src/port/inet_aton.c
 *
 *	这个 inet_aton() 函数取自 GNU C 库，并
 *	被整合到 Postgres 中，以供那些没有这个
 *	例程的标准 C 库的系统使用。
 *
 *	该函数整体提取自 Linux C 库版本 5.3.12 中的
 *	inet_aton.c 文件，该版本源自 GNU C 库，由
 *	Bryan Henderson 于 1996 年 10 月提取。该文件的
 *	版权声明如下。
 */

/*
 * Copyright (c) 1983, 1990, 1993
 *		The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *	  notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *	  notice, this list of conditions and the following disclaimer in the
 *	  documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *	  may be used to endorse或promote products derived from this软件
 *	  without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.  */

#include "c.h"

#include <netinet/in.h>
#include <ctype.h>

#include "port/pg_bswap.h"

/*
 * 检查 "cp" 是否是有效的 ASCII 表示
 * 的 Internet 地址，并转换为二进制地址。
 * 如果地址有效则返回 1，否则返回 0。
 * 这取代了 inet_addr，其返回值
 * 无法区分失败和本地广播地址。
 */
int inet_aton(const char *fc_cp, struct in_addr *fc_addr)
{
	unsigned int fc_val;
	int			fc_base,
				fc_n;
	char		fc_c;
	u_int		fc_parts[4];
	u_int	   *fc_pp = fc_parts;

	for (;;)
	{
		/*
		 * 收集数字，直到到达 ``.''。值的表示方式与 C 相同：0x=十六进制，
		 * 0=八进制，其他=十进制。
		 */
		fc_val = 0;
		fc_base = 10;
		if (*fc_cp == '0')
		{
			if (*++fc_cp == 'x' || *fc_cp == 'X')
				fc_base = 16, fc_cp++;
			else
				fc_base = 8;
		}
		while ((fc_c = *fc_cp) != '\0')
		{
			if (isdigit((unsigned char) fc_c))
			{
				fc_val = (fc_val * fc_base) + (fc_c - '0');
				fc_cp++;
				continue;
			}
			if (fc_base == 16 && isxdigit((unsigned char) fc_c))
			{
				fc_val = (fc_val << 4) +
					(fc_c + 10 - (islower((unsigned char) fc_c) ? 'a' : 'A'));
				fc_cp++;
				continue;
			}
			break;
		}
		if (*fc_cp == '.')
		{
			/*
			 * Internet 格式：a.b.c.d a.b.c	（c 被视为 16 位）
			 * a.b	   （b 被视为 24 位）
			 */
			if (fc_pp >= fc_parts + 3 || fc_val > 0xff)
				return 0;
			*fc_pp++ = fc_val, fc_cp++;
		}
		else
			break;
	}

	/*
	 * 检查是否有多余的垃圾字符。
	 */
	while (*fc_cp)
		if (!isspace((unsigned char) *fc_cp++))
			return 0;

	/*
	 * 根据指定的部分数量组合地址。
	 */
	fc_n = fc_pp - fc_parts + 1;
	switch (fc_n)
	{

		case 1:					/* a -- 32 位 */
			break;

		case 2:					/* a.b -- 8.24 位 */
			if (fc_val > 0xffffff)
				return 0;
			fc_val |= fc_parts[0] << 24;
			break;

		case 3:					/* a.b.c -- 8.8.16 位 */
			if (fc_val > 0xffff)
				return 0;
			fc_val |= (fc_parts[0] << 24) | (fc_parts[1] << 16);
			break;

		case 4:					/* a.b.c.d -- 8.8.8.8 位 */
			if (fc_val > 0xff)
				return 0;
			fc_val |= (fc_parts[0] << 24) | (fc_parts[1] << 16) | (fc_parts[2] << 8);
			break;
	}
	if (fc_addr)
		fc_addr->s_addr = pg_hton32(fc_val);
	return 1;
}
